import { AuthService } from "../auth.service"
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { ConfigService } from '@nestjs/config';
import { JwtPayload } from "src/types/auth-jwtpayload";
@Injectable()
export class RefreshStrategy extends PassportStrategy(Strategy,'refresh-jwt') {

  constructor(
    private readonly configService: ConfigService,
    private readonly authService:AuthService
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromBodyField('refresh'),
      ignoreExpiration: false, 
      secretOrKey: configService.get<string>('REFRESH_TOKEN_SECRET') || 'fallback-secret'
    });
  }


  async validate(payload:JwtPayload) {
    const userId = payload.sub;
    return this.authService.valiadateRefreshToken(userId);
  }
}
